<?php

namespace app\common\model\newhouse;

use think\Model;
use traits\model\SoftDelete;

class School extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'newhouse_school';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'type_text'
    ];

    protected static function init()
    {
        // 执行删除动作后执行
        self::afterDelete(function ($row) {
            // 删除单张图片字段
            $image = $row->image;
            $v = \think\Hook::listen("upload_delete", $image);
            // halt($v);
            // 删除多张图片字段
            $imagesList = explode(",",$row->images);
            foreach ($imagesList as $k => $v) {
                \think\Hook::listen("upload_delete", $v);
            }
        });
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }

    public function getTypeList()
    {
        return ['0' => __('Type 0'), '1' => __('Type 1'), '2' => __('Type 2'), '3' => __('Type 3')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function admin()
    {
        return $this->belongsTo('app\admin\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
